import '../utils/tools.dart';
import 'comparable.dart';
import 'serializable.dart';

class MemberAddressModel extends Serializable implements Equable<int> {
  int id = 0;
  String receiveName = '';
  String mobile = '';

  String province = '';
  String city = '';
  String area = '';
  String street = '';
  String address = '';

  String code = '';

  String lng = '';
  String lat = '';

  bool isDefault = false;

  String get locate => "$lat,$lng";
  set locate(String? latLng) {
    if (latLng != null && latLng.contains(',')) {
      final values = latLng.split(',');
      lat = values[0];
      lng = values[1];
    } else {
      lat = '';
      lng = '';
    }
  }

  String get areaView => "$province $city $area";

  MemberAddressModel();

  MemberAddressModel.fromJson(Map<String, dynamic>? data) {
    if (data == null) return;
    id = Tools.parseInt(data['address_id']);

    receiveName = data['receive_name'] ?? '';
    mobile = data['mobile'] ?? '';

    province = data['province'] ?? '';
    city = data['city'] ?? '';
    area = data['area'] ?? '';
    street = data['street'] ?? '';
    address = data['address'] ?? '';

    code = data['code'] ?? '';
    locate = data['locate'];

    isDefault = Tools.parseInt(data['is_default']) == 1;
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'address_id': id,
      'receive_name': receiveName,
      'mobile': mobile,
      'province': province,
      'city': city,
      'area': area,
      'street': street,
      'address': address,
      'locate': locate,
      'is_default': isDefault ? 1 : 0,
    };
  }

  @override
  int get compareField => id;
}
